package com.scm.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.scm.model.Product;
import com.scm.util.DbUtil;
import com.scm.util.StringUtil;

public class ProductDao extends BaseDao<Product>{
	private static final Logger LOGGER = Logger.getLogger(ProductDao.class);
	public ProductDao() {
		super();
	}

	public ProductDao(Connection conn) {
		super();
		this.conn = conn;
	}
	//修改采购在途数
	public int changePONum(String productCode,int number) throws SQLException {
		PreparedStatement ps = null;
		try {
			String sql = "update product set PONum=PONum+? where productCode = ?";
			LOGGER.debug("[product_reoprt]-->"+sql);
			ps = conn.prepareStatement(sql);
			ps.setInt(1, number);
			ps.setString(2, productCode);
			return ps.executeUpdate();
		} finally {
			DbUtil.close(null, null, ps);
		}
	}
	@Override
	public List<Product> selectData(Map<String,String> querymap) throws SQLException{
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<Product> products = new ArrayList<Product>();
		try {
			StringBuilder sql = new StringBuilder("select * ");
			Map<Integer, String> params = new HashMap<Integer, String>();
			int index = generateQueryCondition(querymap, params, sql);
			sql.append(" limit ?,?");
			ps = conn.prepareStatement(sql.toString());
			for (Integer i : params.keySet()) {
				ps.setObject(i, params.get(i));
			}
			ps.setInt(index++, Integer.parseInt(querymap.get("startNo")));
			ps.setInt(index++, Integer.parseInt(querymap.get("pageSize")));
			rs = ps.executeQuery();
			while (rs.next()) {
				Product product = new Product();
				product.setProductCode(rs.getString("productCode"));
				product.setCategoryID(rs.getInt("categoryID"));
				product.setCreateDate(rs.getString("createDate"));
				product.setName(rs.getString("name"));
				product.setNum(rs.getInt("num"));
				product.setPoNum(rs.getInt("poNum"));
				product.setSoNum(rs.getInt("soNum"));
				product.setPrice(rs.getFloat("price"));
				product.setUnitName(rs.getString("unitName"));
				product.setRemark(rs.getString("remark"));
				products.add(product);
			}
			return products;
		} finally {
			DbUtil.close(conn, rs, ps);
		}

	}
	public int generateQueryCondition(Map<String,String> querymap,final Map<Integer,String> params,final StringBuilder sql) {
		sql.append("from product where 1=1");
		int index = 1;
		String productCode = querymap.get("productCode");
		if(!StringUtil.isEmpty(productCode)) {
			sql.append(" and productCode like concat('%',?,'%')");
			params.put(index++, productCode);
		}
		String name = querymap.get("name");
		if(!StringUtil.isEmpty(name)) {
			sql.append(" and name like concat('%',?,'%')");
			params.put(index++,name);
		}
		String categoryID = querymap.get("categoryID");
		if(!StringUtil.isEmpty(categoryID)) {
			sql.append(" and categoryID = ?");
			params.put(index++, categoryID);
		}
		String createDate = querymap.get("createDate");
		if(!StringUtil.isEmpty(createDate)) {
			sql.append(" and createDate = ?");
			params.put(index++, createDate);
		}
		String num = querymap.get("num");
		if(!StringUtil.isEmpty(num)) {
			sql.append(" and num = ?");
			params.put(index++, num);
		}
		String poNum = querymap.get("poNum");
		if(!StringUtil.isEmpty(poNum)) {
			sql.append(" and poNum = ?");
			params.put(index++, poNum);
		}
		String soNum = querymap.get("soNum");
		if(!StringUtil.isEmpty(soNum)) {
			sql.append(" and soNum = ?");
			params.put(index++, soNum);
		}
		String price = querymap.get("price");
		if(!StringUtil.isEmpty(price)) {
			sql.append(" and price = ?");
			params.put(index++, price);
		}
		String unitName = querymap.get("unitName");
		if(!StringUtil.isEmpty(unitName)) {
			sql.append(" and unitName = ?");
			params.put(index++, unitName);
		}
		String remark = querymap.get("remark");
		if(!StringUtil.isEmpty(remark)) {
			sql.append(" and remark = ?");
			params.put(index++, remark);
		}

		return index;
	}
}
